﻿using FreeSql;

using Tszy.Unlimited.Data.FreeSQL;

namespace Project2.Domain
{
    public abstract class RepositoryBase<TDomainModel> : IRepository<TDomainModel> where TDomainModel : class
    {
        protected readonly IFreeSQLRepository<TDomainModel> _repository;

        public RepositoryBase(IFreeSQLRepository<TDomainModel> repository) => _repository = repository;

        public virtual async Task AddAsync(TDomainModel domainModel)
        {
            await _repository.InsertAsync(domainModel);
        }

        public virtual async Task UpdateAsync(TDomainModel domainModel)
        {
            await _repository.UpdateAsync(domainModel);
        }

        public virtual async Task RemoveAsync(TDomainModel domainModel)
        {
            await _repository.DeleteAsync(domainModel);
        }

        //public virtual async Task<TDomainModel> GetAsync(long identity, string errorMessage = $"获取不到信息")
        //{
        //    //var model = await _repository.Where(item => item.Identity == identity).FirstAsync();

        //    if (model == null)
        //        throw new DomainException(errorMessage);

        //    return model;
        //}

        public virtual ISelect<TDomainModel> Query() => _repository.Select;
        public virtual IFreeSQLRepository<TDomainModel> GetRepository() => _repository;
    }
}